local PANEL = {}
// THIS CODE MAKES ME SAD

/*---------------------------------------------------------
   Name: init
---------------------------------------------------------*/
function PANEL:Init()	
	self:SetSize(83, 83)
	self.Label = vgui.Create ("DLabel", self)
	self:SetKeepAspect(true)
	self:SetDrawBorder(true)
	self.m_Image:SetPaintedManually(true)
end

local PANEL = {}

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()
	self.PanelList = vgui.Create("DPanelList", self)	
		self.PanelList:SetPadding(4)
		self.PanelList:SetSpacing(2)
		self.PanelList:EnableVerticalScrollbar(true)	
	self:BuildList()
end

/*---------------------------------------------------------
   Name: BuildList
---------------------------------------------------------*/
function PANEL:BuildList()
	self.PanelList:Clear()
	
	local Categorised = {}
	
	//The code below is messy and crappy. I will fix it later.
	
	//build class categories
	local teams = team.GetAllTeams()
	Categorised["Class"] = {}
	for k, wtfisthis in pairs(teams) do
		if wtfisthis.SelectableClasses then
			Categorised["Class"][k] = wtfisthis.SelectableClasses
		end
	end
	
	//build entity categories
	local sents = scripted_ents.GetSpawnable()
	sents["sent_ball"] = nil //stupid bouncy ball
	for sentclass, senttable in pairs(sents) do //this code could be better
		if !Categorised[senttable.Category] then Categorised[senttable.Category] = {} end
		Categorised[senttable.Category][sentclass] = senttable
	end
	
	//build weapon categories
	local weps = weapons.GetList()
	for k, weptable in pairs(weps) do
		if weptable.Pistol && weptable.Spawnable && !weptable.Other then
			if !Categorised["Pistols"] then Categorised["Pistols"] = {} end
			Categorised["Pistols"][weptable.ClassName] = weptable
		elseif (weptable.Rifle || weptable.Shotgun || weptable.Sniper) && weptable.Spawnable && !weptable.Other then
			if !Categorised["Shipments"] then Categorised["Shipments"] = {} end
			Categorised["Shipments"][weptable.ClassName] = weptable
		elseif weptable.Other && weptable.Spawnable  then
			if !Categorised["Other Weapons"] then Categorised["Other Weapons"] = {} end
			Categorised["Other Weapons"][weptable.ClassName] = weptable
		elseif weptable.Grenade && weptable.Spawnable && !weptable.Other  then
			if !Categorised["Grenades"] then Categorised["Grenades"] = {} end
			Categorised["Grenades"][weptable.ClassName] = weptable
		end
	end
	
	for CategoryName, v in SortedPairs(Categorised) do
		local Category = vgui.Create("DCollapsibleCategory", self)
		self.PanelList:AddItem(Category)
		Category:SetLabel(CategoryName)
		
		local Content = vgui.Create("DPanelList")
		Category:SetContents(Content)
		Content:EnableHorizontal(true)
		Content:SetDrawBackground(false)
		Content:SetSpacing(2)
		Content:SetPadding(2)
		Content:SetAutoSize(true)
		
		for k, sent in pairs(Categorised[CategoryName]) do
			if sent.TeamIndex || !string.find(sent.ClassName, "base_") then //sent
				if sent.TeamIndex then //class
					local name = sent.Name
					local Icon = vgui.Create("SpawnIcon", self)
					Icon:SetModel(player_manager.TranslatePlayerModel(table.Random(sent.Models)))
					Icon:SetToolTip(false)
					Icon:SetIconSize(64)
					Icon.DoClick = function()
						surface.PlaySound("ui/buttonclickrelease.wav")
						RunConsoleCommand("rpdm_changeclass", sent.TeamIndex) 
					end
					
					local namelabel  = vgui.Create("DLabel", Icon)
						namelabel:SetFont("DefaultSmallDropShadow")
						namelabel:SetTextColor(color_white)
						namelabel:SetText(name)
						namelabel:SetContentAlignment(5)
						namelabel:SetWide(self:GetWide())
						namelabel:AlignBottom(-42)
			
					Content:AddItem(Icon)
				elseif CategoryName == "Drug" then //drug
					local name = sent.PrintName
					local Icon = vgui.Create( "SpawnIcon", self)
					Icon:SetModel("models/combine_apc_destroyed_gib06.mdl")
					Icon:SetToolTip(false)
					Icon.DoClick = function() 
						surface.PlaySound( "ui/buttonclickrelease.wav")
						RunConsoleCommand("rpdm_buydrugshipment", sent.ClassName) 
					end

					local drugs = vgui.Create("DLabel", Icon)
					drugs:SetText(sent.Symbol)
					drugs:SetFont(sent.Font)
					drugs:SetColor(sent.Color)

					if sent.Font == "DrugFont2" then 
						drugs:SetPos(23, 3)
					else
						drugs:SetPos(23, 25)
					end
					drugs:SizeToContents()

					local namelabel  = vgui.Create("DLabel", Icon)
						namelabel:SetFont("DefaultSmall")
						namelabel:SetTextColor(sent.Color)
						namelabel:SetText(name)
						namelabel:SetContentAlignment( 5 )
						namelabel:SetWide(self:GetWide())
						namelabel:AlignBottom(-42)
						
					local pricelabel  = vgui.Create("DLabel", Icon)
						pricelabel:SetFont("TabLarge")
						pricelabel:SetTextColor(Color(0, 255, 0, 200))
						pricelabel:SetText(sent.Cost)
						pricelabel:SetContentAlignment(9)
						pricelabel:SetWide(self:GetWide())

					Content:AddItem(Icon)
				elseif sent.Primary then //weapon
					local name = sent.PrintName
					local Icon = vgui.Create("SpawnIcon", self)
					Icon:SetModel(sent.WorldModel)
					Icon:SetToolTip(false)
					Icon:SetIconSize(64)
					Icon.DoClick = function() 
							surface.PlaySound("ui/buttonclickrelease.wav")
							if(sent.Other || sent.Pistol) then
								RunConsoleCommand("rpdm_buyweapon", sent.ClassName) 					
							elseif((sent.Rifle || sent.Shotgun || sent.Sniper) && !sent.Other) then
								RunConsoleCommand("rpdm_buyweaponshipment", sent.ClassName) 
							elseif(sent.Grenade) then
								RunConsoleCommand("rpdm_buygrenadeshipment", sent.ClassName) 
							end
					end
					
					local namelabel  = vgui.Create("DLabel", Icon)
						namelabel:SetFont("DefaultSmallDropShadow")
						namelabel:SetTextColor(color_white)
						namelabel:SetText(name)
						namelabel:SetContentAlignment(5)
						namelabel:SetWide(self:GetWide())
						namelabel:AlignBottom(-42)
					
					local pricelabel  = vgui.Create("DLabel", Icon)
						pricelabel:SetFont("TabLarge")
						pricelabel:SetTextColor(Color(0, 255, 0, 200))
						pricelabel:SetText(100)
						//pricelabel:SetText(weapons.Get(sent.ClassName).Cost)
						pricelabel:SetContentAlignment(9)
						pricelabel:SetWide(self:GetWide())
			
					Content:AddItem(Icon)
				else //sent
					local name = sent.PrintName
					local Icon = vgui.Create("SpawnIcon", self)
					Icon:SetModel(sent.Model)
					Icon:SetToolTip(false)
					Icon:SetIconSize(64)
					Icon.DoClick = function() 
							surface.PlaySound("ui/buttonclickrelease.wav")
							if(sent.Base == "base_structure") then RunConsoleCommand("rpdm_buysent", sent.ClassName) end
							if(sent.Base == "base_item") then RunConsoleCommand("rpdm_buyitem", sent.ClassName) end
							if(sent.Base == "base_ammo") then RunConsoleCommand("rpdm_buyammo", sent.ClassName) end
					end
					
					local namelabel  = vgui.Create("DLabel", Icon)
						namelabel:SetFont("DefaultSmallDropShadow")
						namelabel:SetTextColor(color_white)
						namelabel:SetText(name)
						namelabel:SetContentAlignment(5)
						namelabel:SetWide(self:GetWide())
						namelabel:AlignBottom(-42)
					
					local pricelabel  = vgui.Create("DLabel", Icon)
						pricelabel:SetFont("TabLarge")
						pricelabel:SetTextColor(Color(0, 255, 0, 200))
						pricelabel:SetText(sent.Cost)
						pricelabel:SetContentAlignment(9)
						pricelabel:SetWide(self:GetWide())
			
					Content:AddItem(Icon)
				end
			end
		end
	end
	self.PanelList:InvalidateLayout()
end

/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()
	self.PanelList:StretchToParent(0, 0, 0, 0)
end

local CreationSheet = vgui.RegisterTable(PANEL, "Panel")

local function CreateContentPanel()
	local ctrl = vgui.CreateFromTable(CreationSheet)
	return ctrl
end

spawnmenu.AddCreationTab("RPDM Items", CreateContentPanel, "gui/silkicons/application", 50)